package com.gitee.feizns.convert.method.valueof;

import com.gitee.feizns.convert.method.MethodSelectionStrategy;
import com.gitee.feizns.convert.method.MethodWrapper;
import com.gitee.feizns.reflect.ClassUtils;

import java.lang.reflect.Method;
import java.util.Queue;

/**
 * @author feizns
 * @since 2019/5/29
 */
public abstract class AbstractMethodSelectionStrategyByParameter implements MethodSelectionStrategy {

    @Override
    public MethodWrapper find(Class<?> targetType, Object original, Method... methods) {
        Queue<Class<?>> classes = ClassUtils.getClassesAndInterfaces(original.getClass());
        while ( classes.isEmpty() == false ) {
            Class<?> item = classes.remove();
            for (Method method : methods) {
                if ( isYou(method, item) )
                    return getInvokeStrategy(method, targetType, original);
            }
        }
        return null;
    }

    /**
     * 寻找具体的方法
     * @param method
     * @param clz
     * @return
     */
    protected abstract boolean isYou(Method method, Class<?> clz);

    /**
     * 方法调用的具体策略
     * @param method
     * @param targetType
     * @param original
     * @return
     */
    protected abstract MethodWrapper getInvokeStrategy(Method method, Class<?> targetType, Object original);

}
